package com.zhang.family_doctor.controller;

import com.github.pagehelper.PageInfo;
import com.google.zxing.WriterException;
import com.zhang.family_doctor.dto.JoinTeamDto;
import com.zhang.family_doctor.dto.TeamDto;
import com.zhang.family_doctor.entity.ApplyJoinTeam;
import com.zhang.family_doctor.entity.R;
import com.zhang.family_doctor.entity.Team;
import com.zhang.family_doctor.entity.TeamJoined;
import com.zhang.family_doctor.service.TeamService;
import com.zhang.family_doctor.vo.TeamIJoinedVO;
import com.zhang.family_doctor.vo.TeamManageVO;
import com.zhang.family_doctor.vo.TeamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * 团队模块
 */
@RestController
@RequestMapping("/team")
@Slf4j
@Api( tags = "团队模块")
public class TeamController {

    @Autowired
    private TeamService teamService;

    @ApiOperation("新增团队")
    @PostMapping("/save")
    public R<String> save(@RequestBody TeamDto teamDto , HttpServletRequest request) throws IOException, WriterException {
        log.info("新增团队：{}",teamDto);
        return teamService.save(teamDto,request);
    }

    @ApiOperation("编辑团队")
    @PostMapping("/updateTeam")
    public R<String> updateTeam(@RequestBody TeamDto teamDto){
        log.info("编辑团队：{}",teamDto);
        return teamService.updateTeam(teamDto);
    }

    @ApiOperation("给团队添加服务包")
    @PostMapping("/savePackages")
    public R<String> savePackages(@RequestBody TeamDto teamDto){
        log.info("给团队添加服务包：{}",teamDto);
        return teamService.savePackages(teamDto);
    }

    @ApiOperation("启用或禁用团队")
    @PostMapping("/enableOrDisable")
    public R<String> enableOrDisable(@RequestBody Team team){
        log.info("启用或禁用团队：{}",team);
        return teamService.enableOrDisable(team);
    }

    @ApiOperation("解散团队")
    @DeleteMapping("/dissolutionTeam")
    public R<String> dissolutionTeam(Integer id){
        log.info("解散团队：{}",id);
        return teamService.dissolutionTeam(id);
    }

    @ApiOperation("申请加入团队")
    @PostMapping("/applyToJoinTeam")
    public R<String> applyToJoinTeam(ApplyJoinTeam applyJoinTeam ,HttpServletRequest request){
        log.info("申请加入团队：{}",applyJoinTeam);
        return teamService.applyToJoinTeam(applyJoinTeam,request);
    }

    @ApiOperation("通过或者拒绝")
    @PostMapping("/passOrReject")
    public R<String> passOrReject(ApplyJoinTeam applyJoinTeam){
        log.info("通过或者拒绝");
        return teamService.passOrReject(applyJoinTeam);
    }

    @ApiOperation("团队成员退出团队")
    @DeleteMapping("/signOutTeam")
    public R<String> signOutTeam(Integer id){
        log.info("团队成员退出团队");
        return teamService.signOutTeam(id);
    }

    @ApiOperation("分页查询我管理的团队")
    @GetMapping("/getTeam")
    public R<PageInfo<TeamVO>> getTeam(Integer pageNum, Integer pageSize,String teamName,Integer mechanismId, HttpServletRequest request){
        log.info("分页查询我管理的团队：");
        return teamService.getTeam(pageNum,pageSize,mechanismId,teamName,request);
    }

    @ApiOperation("分页查询我加入的团队")
    @GetMapping("/getTeamJoined")
    public R<PageInfo<TeamVO>> getTeamJoined(Integer pageNum, Integer pageSize,Integer mechanismId,String teamName, HttpServletRequest request){
        log.info("分页查询我加入的团队：");
        return teamService.getTeamJoined(pageNum,pageSize,mechanismId,teamName,request);
    }

    @ApiOperation("查看我管理的团队详情")
    @GetMapping("/getTeamManageDetails")
    public R<TeamManageVO> getTeamManageDetails(Integer id){
        log.info("查看我管理的团队详情：{}",id);
        return teamService.getTeamManageDetails(id);
    }

    @ApiOperation("查看我加入的团队详情")
    @GetMapping("/getTeamIJoinedDetails")
    public R<TeamIJoinedVO> getTeamIJoinedDetails(Integer id){
        log.info("查看我加入的团队详情：{}",id);
        return teamService.getTeamIJoinedDetails(id);
    }

    @ApiOperation("根据机构id查询团队")
    @GetMapping("/getTeamByMechanismId")
    public R<List<Team>> getTeamByMechanismId(Integer mechanismId){
        log.info("根据机构id查询团队：{}",mechanismId);
        return teamService.getTeamByMechanismId(mechanismId);
    }

    @ApiOperation("根据团队id查询队员")
    @GetMapping("/getTeamJoinedByTeamId")
    public R<List<TeamJoined>> getTeamJoinedByTeamId(Integer teamId){
        log.info("根据团队id查询队员：{}",teamId);
        return teamService.getTeamJoinedByTeamId(teamId);
    }

}
